package com.yang.staff.listen;

import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class WordsListen {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    RestHighLevelClient client;

    @Autowired
    RestClient restClient;

    @RabbitListener(queues = "words_prohibit")
    @RabbitHandler
    public void sync(String mesMap) throws IOException {
        Map<String, String> map = JSONObject.parseObject(mesMap, Map.class);
        IndexRequest indexRequest = new IndexRequest("words", "words_prohibit");
        indexRequest.source(map);
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(indexResponse.getResult());
    }
}
